// pages/familydoctor/perfect/perfect.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['北京市', '北京市', '东城区'],
    crowd: [],
    selected: 1,
    username: "",
    idnumber: '',
    phone: "",
    address: '',
    household: '',
    arr: []
  },
  //获取用户姓名:
  username: function(e) {
    this.setData({
      username: e.detail.value
    })
  },
  //获取身份证号
  idnumber: function(e) {
    this.setData({
      idnumber: e.detail.value
    })
  },
  //获取手机号
  phone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //详细地址
  address: function(e) {
    this.setData({
      address: e.detail.value
    })
  },
  //获取户主身份证号
  household: function(e) {
    this.setData({
      household: e.detail.value
    })
  },
  //城市地区
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  choose: function(e) {
    var id = e.currentTarget.dataset.id;
    console.log(id)
    this.setData({
      selected: id
    })
  },
  select: function(e) {
    console.log(e)
    var id = e.currentTarget.dataset.id;
    var crowd = this.data.crowd
    var index = e.currentTarget.dataset.index
    crowd[index].isshow = !crowd[index].isshow
    this.setData({
      crowd: crowd
    })
    var arr = []
    for (var i = 0; i < crowd.length; i++) {
      if (crowd[i].isshow == true) {
        arr.push(crowd[i])
      }
    }
    this.setData({
      arr: arr
    })
  },
  next: function(e) {
    var that = this
    var t_id = that.data.t_id
    var sid = that.data.sid
    var key = that.data.key
    var name = that.data.name
    var j_id = that.data.j_id
    var names = that.data.names
    var teamname = that.data.teamname
    var arr = that.data.arr
    var zid = that.data.zid
    var reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/; //验证姓名正则
    var secondID = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/; //第二代身份证号
    var phone = /^1[3456789]\d{9}$/;
    if (that.data.names == "") {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none'
      })
    } else if (that.data.numcard == "") {
      wx.showToast({
        title: '份证号不能为空',
        icon: 'none'
      })
    } else if (that.data.tel == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      })
    } else if (!phone.test(that.data.tel)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
    } else if (that.data.address == "") {
      wx.showToast({
        title: '地址不能为空',
        icon: 'none'
      })
    } else if (that.data.selected == 1 && this.data.household == "") {
      wx.showToast({
        title: '户主身份证号不能为空',
        icon: 'none'
      })
    } else if (arr.length == 0) {
      wx.showToast({
        title: '请选择人群',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: '/hyb_yl/doctor/pages/familydoctor/servicepackage/servicepackage?arr=' + JSON.stringify(that.data.arr) + '&t_id=' + t_id + '&sid=' + sid + '&key=' + key + '&name=' + name + '&j_id=' + j_id + '&names=' + names + '&teamname=' + teamname + '&hzsfz=' + this.data.household + '&ifhz=' + this.data.selected + '&zid=' + zid,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var t_id = options.t_id
    var sid = options.sid
    var key = options.key
    var name = options.name
    var j_id = options.j_id
    var names = options.names
    var teamname = options.teamname
    var zid = options.zid
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    app.util.request({
      'url': 'entry/wxapp/User.ifself',
      'data': {
        openid: wx.getStorageSync('openid'),
      },
      success: function(res) {
        console.log(res)
        var regionb = res.data.region.split(",")
        console.log(regionb)
        that.setData({
          names: res.data.names,
          j_id: res.data.j_id,
          region: regionb,
          datetime: res.data.datetime,
          numcard: res.data.numcard,
          tel: res.data.tel
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/Office.fllist',
      success(res) {
        var list = res.data
        for (var i = 0; i < list.length; ++i) {
          list[i].isshow = false
        }
        that.setData({
          crowd: res.data,

        })
        console.log(res)
      }
    })
    that.setData({
      bgc: bgc,
      t_id: t_id,
      sid: sid,
      key: key,
      name: name,
      j_id: j_id,
      names: names,
      teamname: teamname,
      zid: zid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})